package com.gzhuxj201.controller;

import com.gzhuxj201.entity.*;
import com.gzhuxj201.service.HomeWorkService;
import com.gzhuxj201.service.TeacherService;
import com.gzhuxj201.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/homeworks")
@Slf4j
public class HomeWorkController {
    @Autowired
    private HomeWorkService homeWorkService;

    @GetMapping("/forStudentPage")
    public Result selectByClassId(Integer page,Integer pageSize,String courseId,String name){

        PageBean pageBean= homeWorkService.selectByClassId(page,pageSize,courseId,name);
        return Result.success(pageBean);
    }

    @GetMapping
    public Result selectByTno(@RequestParam(required = false) String courseId, HttpSession session){
        //从session中获取教师用户的tno
        Teacher teacher = (Teacher)session.getAttribute("user");
        String tno = teacher.getTno();

       List<HomeWorkDto> homeWorkDtoList= homeWorkService.selectByTno(tno,courseId);
        return Result.success(homeWorkDtoList);
    }

    @PostMapping
    public Result add(@RequestBody HomeWork homeWork){
         int res= homeWorkService.add(homeWork);
        return  Result.success();
    }

    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id){
       int res= homeWorkService.deleteById(id);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result selectById(@PathVariable Integer id){
        HomeWork homeWork=homeWorkService.selectById(id);
        return Result.success(homeWork);
    }

    @PutMapping
    public Result update(@RequestBody HomeWork homeWork){
        int res=homeWorkService.update(homeWork);
        return Result.success();
    }


}
